<?php

class Db {
	static private $_instance;
	static private $_connectSource;
	private $_dbConfig = array(
		'host' => '127.0.0.1',
		'user' => 'root',
		'password' => 'root',
		'database' => 'bd_news',
	);

	private function __construct() {
	}

	static public function getInstance() {
		if(!(self::$_instance instanceof self)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	public function connect() {

		if(!self::$_connectSource) {

			self::$_connectSource = @mysqli_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']);

			if(!self::$_connectSource) {
//				throw new Exception('mysql connect error ' . mysqli_error(self::$_connectSource));
				die('mysql connect error' . mysqli_error(self::$_connectSource));
			}

			mysqli_select_db( self::$_connectSource,$this->_dbConfig['database']);
			mysqli_query( self::$_connectSource,"set names UTF8");
		}
		return self::$_connectSource;
	}

}




//$connect = Db::getInstance()->connect();
//
//$sql = "select * from news";
//$result = mysqli_query($connect,$sql);
//
//var_dump($result);
//
//if ($result->num_rows > 0) {
//	// 输出每行数据
//	while($row = $result->fetch_assoc()) {
//		echo "<br> id: ". $row["id"]. " - 标题: ". $row["title"]. " " . $row["des"];
//	}
//} else {
//	echo "0 results";
//}
//$connect->close();
